package com.hsy.bibc.modules.common;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.deparse.dpframe.ui.fragment.DPBaseFragment;
import com.deparse.dpframe.utils.DPFileUtil;
import com.hsy.bibc.R;

import java.io.File;


/**
 * Created by hsy on 2016/3/23.
 */
public abstract class CommonBaseFragment extends DPBaseFragment implements IFragmentUpdateListener {

    private View networkFailureView;
    private View networkRefreshButton;

    /**
     * Fragment当前状态是否可见
     */
    protected boolean isVisible;


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }


    /**
     * 可见
     */
    protected void onVisible() {
        onFragmentUpdate();
    }

    /**
     * 不可见
     */
    protected void onInvisible() {

    }

    @Override
    public void onResume() {
        super.onResume();
//        JPushInterface.onFragmentResume(getAttchedActivity(),null);
    }

    @Override
    public void onPause() {
        super.onPause();
//        JPushInterface.onFragmentPause(getAttchedActivity(),null);
    }

    @Override
    public void onDestroyView() {
        File cacheDir = new File(DPFileUtil.getCacheDir(getContext()));
        if (cacheDir.exists() && cacheDir.isDirectory()) {
            DPFileUtil.emptyDir(cacheDir);
        }
        super.onDestroyView();
        System.gc();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        networkFailureView = inflater.inflate(R.layout.http_error_layout, null);
        networkRefreshButton = networkFailureView.findViewById(R.id.network_refresh);
        networkRefreshButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onNetworkRefresh();
            }
        });
        networkFailureView.setVisibility(View.GONE);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        addContentView(networkFailureView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    public void showNetworkFailureScreen() {
        getRootView().setVisibility(View.GONE);
        networkFailureView.setVisibility(View.VISIBLE);
    }

    public void hideNetworkFailureScreen() {
        getRootView().setVisibility(View.VISIBLE);
        networkFailureView.setVisibility(View.GONE);
    }

    protected void onNetworkRefresh() {

    }
}
